package com.jdwx.opengl.mvp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jdwx.opengl.R;
import com.jdwx.opengl.face.dface.activity.FaceConfigActivity;
import com.jdwx.opengl.face.dface.activity.FaceRegisterActivity;
import com.jdwx.opengl.face.utils.DFacePermission;
import com.jdwx.opengl.mvp.model.entity.Student;
import com.jdwx.opengl.mvp.presenter.LoginPresenterImpl;
import com.jdwx.opengl.mvp.ui.impl.LoginView;
import com.jdwx.opengl.serialport.SerialPort;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements LoginView {

    private LoginPresenterImpl presenter;
    private ProgressBar progress;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        presenter = new LoginPresenterImpl( this );
        progress = findViewById(R.id.progress);
        //点击登录
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student s = new Student();
                s.name = ((EditText)findViewById(R.id.name)).getText().toString();
                s.pass = ((EditText)findViewById(R.id.pass)).getText().toString();
                presenter.validateCredentials( s );
            }
        });
        findViewById(R.id.login1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent(MainActivity.this, FaceConfigActivity.class) );
            }
        });
        findViewById(R.id.login2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent(MainActivity.this, FaceRegisterActivity.class) );
            }
        });

        new DFacePermission(this).Permission();


        // 16    , 0.75
        new HashMap<String,Object>(1, (float) 0.75);


        try {
            new SerialPort(new File("/dev/ttyS3"),9600,0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showProgress() {
        progress.setVisibility(View.VISIBLE);

        Log.e( "mvp","显示进度条" );

    }

    @Override
    public void hideProgress() {
        progress.setVisibility(View.GONE);
        Log.e( "mvp","关闭进度条" );

    }

    @Override
    public void setUsernameError() {

        Log.e( "mvp","用户名错误" );

    }

    @Override
    public void setPasswordError() {
        Log.e( "mvp","密码错误" );

    }

    @Override
    public void showSuccess() {

        Log.e( "mvp","登录成功" );

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
